package JavaFiler;

/**
 *
 * @author Per Stian Hoff & Gjerde
 */
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class dbConnection implements Serializable {

    private Connection conn;
    private String url = "jdbc:derby://localhost:1527/Mattespill;user=kalle;password=kalle";

    /**
     * Oppretter en forbindelse til databasen
     * @return en Connection for forbindelsen
     */
    synchronized public Connection getCon() {

        try {
            if (conn != null && !conn.isClosed()) {
                return conn;
            }
        } catch (SQLException e1) {
            e1.printStackTrace();
        }

        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            conn = DriverManager.getConnection(url);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * Kobler ned forbindelsen
     */
    synchronized public void disconnect() {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
